﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;
using System.Web.Security;

namespace MAT.Controllers
{
    class ctrChangePassword
    {
        private frmChangePassword _frmChangePassword = null;

        public frmChangePassword FrmChangePassword
        {
            get { return _frmChangePassword; }
            set { _frmChangePassword = value; }
        }
        
        MAT_Context context = MATContext.getContext();
        //check CurrentPassword
        private bool checkCurrentPassword(TextBox current)
        {
            string currentPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(current.Text, "MD5");
            LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.USERNAME == MAT.Controllers.ctrLogin.Username
                                                            && log.PASSWORDS == currentPassword);
            if (login != null)
                return true;
            else
            {
                MessageBox.Show("Current password isn't match! Try again!");
                current.Text = "";
                current.Focus();
                return false;
            }
        }
        
        private bool checkLength(TextBox pw1)
        {
            if (pw1.Text.Length < 6 || pw1.Text.Length > 12)
            {
                MessageBox.Show("New password must have from 6 to 12 digits");
                pw1.Text = "";
                pw1.Focus();
                return false;
            }
            return true;
        }

        private bool checkPasswordMatch(TextBox pw1, TextBox pw2)
        {
            if (pw1.Text.Equals(pw2.Text))
                return true;
            else
            {
                MessageBox.Show("New passwords aren't match! Please retype new password!");
                pw1.Text = "";
                pw2.Text = "";
                pw1.Focus();
                return false;
            }
        }

        private bool checkPasswordMatch1(TextBox pw1, TextBox pw2)
        {
            if (pw1.Text.Equals(pw2.Text))
            {

                MessageBox.Show("New passwords match current password! Please retype new password!");
                pw1.Text = "";
                pw2.Text = "";
                pw1.Focus();
                return false;
            }
            else
            
            
                return true;
            
        }
        public void btnChange_Click()
        {
            if (checkCurrentPassword(_frmChangePassword.txtCurrentPassword))
            {
                if (checkLength(_frmChangePassword.txtNewPassword))
                {
                    if (checkPasswordMatch(_frmChangePassword.txtNewPassword, _frmChangePassword.txtConfirmPassword))
                    {
                        if (checkPasswordMatch1(_frmChangePassword.txtCurrentPassword, _frmChangePassword.txtNewPassword))
                        {
                            List<LOG_IN> log = context.LOG_IN.ToList();
                            LOG_IN login = null;
                            for (int i = 0; i < log.Count; i++)
                            {
                                if ((log[i].USERNAME).ToLower().Equals((MAT.Controllers.ctrLogin.Username).ToLower()))
                                {
                                    login = log[i];
                                }
                            }
                            if (login != null)
                            {
                                login.PASSWORDS = FormsAuthentication.HashPasswordForStoringInConfigFile(_frmChangePassword.txtNewPassword.Text, "MD5");
                                MessageBox.Show("\tSUCCESSFUL! \nYour password has just been changed!");
                                context.SaveChanges();
                                _frmChangePassword.Close();
                            }
                        }
                    }
                }
            }
        }

        public void btnClose_Click()
        {
            _frmChangePassword.Close();
        }

        public void kd_change(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnChange_Click();
        }
    }
}
